package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天拜访---促销数据采集bean解析类
 * @author wuz  @date 2011-5-28 11:08:08
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class DataCollectSaleInfoBeanParser {
	public static List<DataCollectSaleInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        DataCollectSaleInfoBean bean = null;
        List<DataCollectSaleInfoBean> list = null;
        
        UnitInfoBean unitInfo = null;
    	SaleTypeBean saleType = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<DataCollectSaleInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	bean = new DataCollectSaleInfoBean();
                	unitInfo = new UnitInfoBean();
                	saleType = new SaleTypeBean();
                } else if (null != list && null != bean) {
                    if("BISMT_PROM".equals(parser.getName())) {
                    	bean.setBismt_prom(parser.nextText());
                    } else if("BDATE".equals(parser.getName())) {
                    	bean.setBdate(parser.nextText());
                    } else if("EDATE".equals(parser.getName())) {
                    	bean.setEdate(parser.nextText());
                    } else if("PROMPRICE".equals(parser.getName())) {
                    	bean.setPromprice(parser.nextText());
                    } else if("PSALEPRICE".equals(parser.getName())) {
                    	bean.setPsaleprice(parser.nextText());
                    } else if("JG".equals(parser.getName())) {
                    	saleType.setJg(parser.nextText());
                    } else if("JGNAME".equals(parser.getName())) {
                    	saleType.setJgname(parser.nextText());
                    } else if("TG".equals(parser.getName())) {
                    	bean.setTg(parser.nextText());
                    } else if("TICKET".equals(parser.getName())) {
                    	bean.setTicket(parser.nextText());
                    } else if("PROMREMARK".equals(parser.getName())) {
                    	bean.setPromremark(parser.nextText());
                    } else if("UNIT".equals(parser.getName())) {
                    	unitInfo.setUnit(parser.nextText());
                    } else if("UNITNAME".equals(parser.getName())) {
                    	unitInfo.setUnitname(parser.nextText());
                    } else if("VSDATE".equals(parser.getName())) {
                    	bean.setVsdate(parser.nextText().substring(0, 10));
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                	if (null != list && bean != null) {
                		bean.setSaleType(saleType);
                		bean.setUnitInfo(unitInfo);
                		list.add(bean);
                		bean = null;
                		saleType = null;
                		unitInfo = null;
                	} 
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
	
	public static String createXML(LoginInfoBean loginInfo, DataCollectBaseInfoBean bean,
			List<DataCollectSaleInfoBean> list,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, list.size() + "");
        DataCollectSaleInfoBean dataCollectSaleInfo = null;
        for(int i = 0; i < list.size(); i++) {
        	dataCollectSaleInfo = list.get(i);
        	xmlSerializer.startTag("", XMLStatic.RECORDS);
            xmlSerializer.startTag("", XMLStatic.WAYID);
            xmlSerializer.text(bean.getMyWay().getWayid());
    		xmlSerializer.endTag("", XMLStatic.WAYID);
    		xmlSerializer.startTag("", XMLStatic.PARTNER);
            xmlSerializer.text(bean.getCustomerInfo().getPartner());
    		xmlSerializer.endTag("", XMLStatic.PARTNER);
    		xmlSerializer.startTag("", XMLStatic.PERNR);
            xmlSerializer.text(loginInfo.getPernr());
    		xmlSerializer.endTag("", XMLStatic.PERNR);
    		xmlSerializer.startTag("", XMLStatic.VSDATE);
            xmlSerializer.text(null == bean.getVsdate() ? "" : bean.getVsdate());
    		xmlSerializer.endTag("", XMLStatic.VSDATE);
    		xmlSerializer.startTag("", XMLStatic.BISMT_PROM);
            xmlSerializer.text(null == dataCollectSaleInfo.getBismt_prom() ? "" : dataCollectSaleInfo.getBismt_prom());
    		xmlSerializer.endTag("", XMLStatic.BISMT_PROM);
    		xmlSerializer.startTag("", XMLStatic.PSALEPRICE);
            xmlSerializer.text(null == dataCollectSaleInfo.getPsaleprice() ? "" : dataCollectSaleInfo.getPsaleprice());
    		xmlSerializer.endTag("", XMLStatic.PSALEPRICE);
    		xmlSerializer.startTag("", XMLStatic.PROMPRICE);
            xmlSerializer.text(null == dataCollectSaleInfo.getPromprice() ? "" : dataCollectSaleInfo.getPromprice());
    		xmlSerializer.endTag("", XMLStatic.PROMPRICE);
    		xmlSerializer.startTag("", XMLStatic.BDATE);
            xmlSerializer.text(null == dataCollectSaleInfo.getBdate() ? "" : dataCollectSaleInfo.getBdate());
    		xmlSerializer.endTag("", XMLStatic.BDATE);
    		xmlSerializer.startTag("", XMLStatic.EDATE);
            xmlSerializer.text(null == dataCollectSaleInfo.getEdate() ? "" : dataCollectSaleInfo.getEdate());
    		xmlSerializer.endTag("", XMLStatic.EDATE);
    		xmlSerializer.startTag("", XMLStatic.JG);
            xmlSerializer.text(dataCollectSaleInfo.getSaleType().getJg());
    		xmlSerializer.endTag("", XMLStatic.JG);
    		xmlSerializer.startTag("", XMLStatic.TG);
            xmlSerializer.text(null == dataCollectSaleInfo.getTg() ? "" : dataCollectSaleInfo.getTg());
    		xmlSerializer.endTag("", XMLStatic.TG);
    		xmlSerializer.startTag("", XMLStatic.TICKET);
            xmlSerializer.text(null == dataCollectSaleInfo.getTicket() ? "" : dataCollectSaleInfo.getTicket());
    		xmlSerializer.endTag("", XMLStatic.TICKET);
    		xmlSerializer.startTag("", XMLStatic.PROMREMARK);
            xmlSerializer.text(null == dataCollectSaleInfo.getPromremark() ? "" : dataCollectSaleInfo.getPromremark());
    		xmlSerializer.endTag("", XMLStatic.PROMREMARK);
    		xmlSerializer.startTag("", XMLStatic.UNIT);
            xmlSerializer.text(dataCollectSaleInfo.getUnitInfo().getUnit());
    		xmlSerializer.endTag("", XMLStatic.UNIT);
    		xmlSerializer.endTag("", XMLStatic.RECORDS);
        }
        
        if(0 == list.size()) {
        	xmlSerializer.text("");
        }
        
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
}
